package com.jingbian.cppccbackend.service.impl.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jingbian.cppccbackend.entity.PublicOpinionAttachments;
import com.jingbian.cppccbackend.entity.PublicOpinions;
import com.jingbian.cppccbackend.mapper.admin.adminPublicOpinionsMapper;
import com.jingbian.cppccbackend.service.admin.adminPublicOpinionsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingbian.cppccbackend.entity.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 社情民意信息表 服务实现类
 * </p>
 *
 * @author yezi
 * @since 2025-04-26
 */
@Service
public class adminPublicOpinionsServiceImpl extends ServiceImpl<adminPublicOpinionsMapper, PublicOpinions> 
        implements adminPublicOpinionsService {

    @Autowired
    private adminPublicOpinionsMapper adminPublicOpinionsMapper;

    @Override
    public PublicOpinions getPublicOpinionById(Integer opinionId) {
        return adminPublicOpinionsMapper.getPublicOpinionById(opinionId);
    }

    @Override
    public PageVO<PublicOpinions> getAllPublicOpinionsWithDetails(
            Integer pageNum,
            Integer pageSize,
            String status,
            String opinionType,
            String startDate,
            String endDate,
            String sort,
            String department,
            String searchText,
            Integer pageStatus
    ) {
        PageVO<PublicOpinions> pb = new PageVO<>();
        PageHelper.startPage(pageNum, pageSize);
        List<PublicOpinions> publicOpinions = adminPublicOpinionsMapper.getAllPublicOpinionsWithDetails(
                status, opinionType, startDate, endDate, sort, department, searchText, pageStatus);
        Page<PublicOpinions> pag = (Page<PublicOpinions>) publicOpinions;
        pb.setTotal(pag.getTotal());
        pb.setItems(pag.getResult());
        return pb;
    }

    @Override
    public void updatePublicOpinionStatus(Integer opinionId, String status, Boolean isOr, Integer pageStatus, 
            String department, String handing_result, List<String> filePaths, String fileName, String size) {
        adminPublicOpinionsMapper.updatePublicOpinionStatus(opinionId, status, isOr, pageStatus, department, handing_result);
        
        if (pageStatus == 301 && filePaths != null && !filePaths.isEmpty()) {
            for (String filePath : filePaths) {
                PublicOpinionAttachments attachment = new PublicOpinionAttachments();
                attachment.setPublicOpinionId(opinionId);
                attachment.setFilePath(filePath);
                attachment.setUploadTime(LocalDateTime.now());
                attachment.setFileName(fileName);
                attachment.setSize(size);
                attachment.setIsHandle(true);
                adminPublicOpinionsMapper.insertHandlingAttachment(attachment);
            }
        }
    }

    @Override
    public void senBack(Integer opinionId, String status, Boolean isOr, Integer pageStatus, String department) {
        adminPublicOpinionsMapper.sendBack(opinionId, status, isOr, pageStatus, department);
    }

    @Override
    public List<PublicOpinions> searchPublicOpinions(String searchText) {
        if (StringUtils.isEmpty(searchText)) {
            return adminPublicOpinionsMapper.selectList(null);
        }
        return adminPublicOpinionsMapper.searchPublicOpinions(searchText);
    }

    @Override
    public Map<String, Object> getPublicOpinionStatistics() {
        return Map.of(
            "totalPublicOpinions", adminPublicOpinionsMapper.getTotalPublicOpinions(),
            "newPublicOpinionsYesterday", adminPublicOpinionsMapper.getNewPublicOpinionsYesterday(),
            "completedPublicOpinions", adminPublicOpinionsMapper.getCompletedPublicOpinions()
        );
    }

    @Override
    public List<Map<String, Object>> getPublicOpinionTypeDistribution() {
        return adminPublicOpinionsMapper.getPublicOpinionTypeDistribution();
    }

    @Override
    public List<Map<String, Object>> getPublicOpinionTrend(String startDate, String endDate) {
        return adminPublicOpinionsMapper.getPublicOpinionTrend(startDate, endDate);
    }

    @Override
    public void publicity(Integer opinionId, String status, Boolean isOr) {
        adminPublicOpinionsMapper.publicity(opinionId, status, isOr);
    }

    @Override
    public List<PublicOpinions> getPublicOpinionsExportList(
            String status, String opinionType, String startDate, String endDate,
            String sort, String department, String searchText, Integer pageStatus) {
        return adminPublicOpinionsMapper.getAllPublicOpinionsWithDetails(
                status, opinionType, startDate, endDate, sort, department, searchText, pageStatus);
    }
}
